---
title: "Cohesion"
author: "`r Sys.info()['user']`"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 10, fig.height = 5)
require(LSX)

lss_en <- readRDS("lss_simulation_en.RDS")
dat_en <- readRDS("data_simulation_en.RDS")
coh_en <- cohesion(lss_en, 10)
dat_en <- merge(dat_en, coh_en$component, all.x = TRUE)
dat_en$r2_smoothed <- ksmooth(dat_en$k, dat_en$r2, kernel = "normal",
                              bandwidth = 10)$y

lss_ja <- readRDS("lss_simulation_ja.RDS")
dat_ja <- readRDS("data_simulation_ja.RDS")
coh_ja <- cohesion(lss_ja, 10)
dat_ja <- merge(dat_ja, coh_ja$component, all.x = TRUE)
dat_ja$r2_smoothed <- ksmooth(dat_ja$k, dat_ja$r2, kernel = "normal",
                              bandwidth = 10)$y
```

```{r}
cor(dat_en$r2, dat_en$smoothed)
cor(dat_ja$r2, dat_ja$smoothed)
```


```{r, fig.width = 10, fig.height = 6}
par(mfrow = c(2, 2), mar = c(2, 4, 4, 1), font.main = 1)

plot(dat_en$k, dat_en$r2, type = "p", ylim = c(0.3, 0.8), pch = 16,
     ylab = "Correlation", xlab = "", main = "Economy (English)", col = "gray")
grid()
lines(dat_en$k, dat_en$r2_smoothed, col = "black")

plot(dat_ja$k, dat_ja$r2, type = "p", ylim = c(0.3, 0.8), pch = 16, 
     ylab = "Correlation", xlab = "", main = "Politics (Japanese)", col = "gray")
grid()
lines(dat_ja$k, dat_ja$r2_smoothed, col = "black")

par(mar = c(4, 4, 1, 1))

plot(dat_en$k, dat_en$raw, col = "gray", type = "p", xlim = c(100, 400), pch = 16,
     ylab = "Cohesion", xlab = "k")
grid()
lines(dat_en$k, dat_en$smoothed)

plot(dat_ja$k, dat_ja$raw, col = "gray", type = "p", xlim = c(100, 400), pch = 16,
     ylab = "Cohesion", xlab = "k")
grid()
lines(dat_ja$k, dat_ja$smoothed)
```


```{r}
head(dat_en[order(dat_en$r2, decreasing = TRUE),] , 5)
tail(dat_en[order(dat_en$r2, decreasing = TRUE),] , 5)
```

```{r}
head(dat_ja[order(dat_ja$r2, decreasing = TRUE),] , 5)
tail(dat_ja[order(dat_ja$r2, decreasing = TRUE),] , 5)
```

```{r}
cor(dat_en$r2, dat_en$raw)
```

```{r}
cor(dat_ja$r2, dat_ja$raw)
```

